#include "Common.h"
#include "UIManager.h"
#include "DXUTgui.h"
#include "Render.h"
#include "Root.h"
#include "RenderWindow.h"
#include "InGameMainWindow.h"
#include "Window.h"


UIManager::UIManager()
	: mDialogResourceMgr(NULL)
	, mWindowWidth(0)
	, mWindowHeight(0)
{
}

UIManager::~UIManager()
{
	// close all of the window.
	Window::CloseAll();

	SAFE_DELETE(mDialogResourceMgr);
}

bool UIManager::Init()
{
	mDialogResourceMgr = new CDXUTDialogResourceManager();
	mDialogResourceMgr->OnD3D9CreateDevice(Render::Instance().getDevice());

	mWindowWidth = Root::Instance().getRenderWindow()->getWidth();
	mWindowHeight = Root::Instance().getRenderWindow()->getHeight();

	return true;
}

bool UIManager::MsgProc(void* hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	switch (msg)
	{
	case WM_KEYDOWN:
		if (wParam == VK_ESCAPE)
		{
			if (InGameMainWindow::Exist())
				InGameMainWindow::Instance().OnInGameMenu();
		}
		break;
	case WM_SIZE:
		OnWindowResized(LOWORD(lParam), HIWORD(lParam));
		break;
	}

	return mDialogResourceMgr->MsgProc((HWND)hWnd, msg, wParam, lParam);
}

void UIManager::Render(float elapsedTime)
{
	if (mDialogResourceMgr == NULL)
		return;

	mDialogResourceMgr->OnRender(elapsedTime);
}

void UIManager::OnWindowResized(int width, int height)
{
	mWindowWidth = width;
	mWindowHeight = height;
}